package com.ruicar.afs.cloud.afscase.interfacerecord.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.interfacerecord.entity.CaseInterfaceRecord;
import com.ruicar.afs.cloud.afscase.interfacerecord.mapper.CaseInterfaceRecordMapper;
import com.ruicar.afs.cloud.afscase.interfacerecord.service.CaseOcrInterfaceRecordService;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.BankCardOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.IdCardBackOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.IdCardFrontOcrDiscernVo;
import com.ruicar.afs.cloud.afscase.interfacerecord.vo.InvoiceCarOcrDiscernVo;
import com.ruicar.afs.cloud.image.condition.OcrDiscernCondition;
import com.ruicar.afs.cloud.image.service.OcrDiscernService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description 接口调用记录
 * @Author rongji.zhang
 * @Date 2020/08/14 10:53
 */
@Service
@Slf4j
@AllArgsConstructor
public class CaseOcrInterfaceRecordServiceImpl extends ServiceImpl<CaseInterfaceRecordMapper, CaseInterfaceRecord>
        implements CaseOcrInterfaceRecordService {

    /**
     * OCR识别
     */
    private OcrDiscernService ocrDiscernService;

    /**
     * @param condition
     * @Description:获取机动车发票OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: InvoiceCarOcrDiscernVo
     */
    @Override
    public InvoiceCarOcrDiscernVo getInvoiceCarOcrDiscern(OcrDiscernCondition condition) {
        JSONObject invoiceCar = ocrDiscernService.invoiceCarOcrDiscern(condition);
        if(invoiceCar !=null) {
            log.info("*************************************调用接口记录入库开始**********************************");
            CaseInterfaceRecord caseInterfaceRecord = new CaseInterfaceRecord();
            caseInterfaceRecord.setBusinessNo(condition.getCaseCode());
            caseInterfaceRecord.setInterfaceType("OcrDiscern");
            caseInterfaceRecord.setReturnJson(invoiceCar.toString());
            this.save(caseInterfaceRecord);
            log.info("*************************************调用接口记录入库结束**********************************");
            InvoiceCarOcrDiscernVo invoiceCarOcrDiscernVo = new InvoiceCarOcrDiscernVo();
            if (invoiceCar.getString("code").equals("200")) {
                invoiceCarOcrDiscernVo.setId(invoiceCar.getJSONObject("data").getString("id"));
                invoiceCarOcrDiscernVo.setVehicleInvoiceMd5(invoiceCar.getJSONObject("data").getString("vehicleInvoiceMd5"));
                invoiceCarOcrDiscernVo.setCreateTime(invoiceCar.getJSONObject("data").getDate("createTime"));
                invoiceCarOcrDiscernVo.setInvoiceNum(invoiceCar.getJSONObject("data").getString("invoiceNum"));
                invoiceCarOcrDiscernVo.setInvoiceCode(invoiceCar.getJSONObject("data").getString("invoiceCode"));
                invoiceCarOcrDiscernVo.setSaler(invoiceCar.getJSONObject("data").getString("saler"));
                invoiceCarOcrDiscernVo.setMachineCode(invoiceCar.getJSONObject("data").getString("machineCode"));
                invoiceCarOcrDiscernVo.setVinNum(invoiceCar.getJSONObject("data").getString("vinNum"));
                invoiceCarOcrDiscernVo.setTaxRate(invoiceCar.getJSONObject("data").getString("taxRate"));
                invoiceCarOcrDiscernVo.setPriceTaxLow(invoiceCar.getJSONObject("data").getString("priceTaxLow"));
                try {
                    invoiceCarOcrDiscernVo.setInvoiceDate(invoiceCar.getJSONObject("data").getDate("invoiceDate"));
                } catch (Exception e){
                    String time = "9999-12-31";
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        Date date = sdf.parse(time);
                        invoiceCarOcrDiscernVo.setInvoiceDate(date);
                    } catch (ParseException exception) {
                        // TODO Auto-generated catch block
                        exception.printStackTrace();
                    }
                }
                invoiceCarOcrDiscernVo.setPrice(invoiceCar.getJSONObject("data").getString("price"));
                invoiceCarOcrDiscernVo.setSalerBank(invoiceCar.getJSONObject("data").getString("salerBank"));
                invoiceCarOcrDiscernVo.setTaxAuthor(invoiceCar.getJSONObject("data").getString("taxAuthor"));
                invoiceCarOcrDiscernVo.setManuModel(invoiceCar.getJSONObject("data").getString("manuModel"));
                invoiceCarOcrDiscernVo.setCertificateNum(invoiceCar.getJSONObject("data").getString("certificateNum"));
                invoiceCarOcrDiscernVo.setPurchaser(invoiceCar.getJSONObject("data").getString("purchaser"));
                invoiceCarOcrDiscernVo.setVehicleType(invoiceCar.getJSONObject("data").getString("vehicleType"));
                invoiceCarOcrDiscernVo.setPriceTax(invoiceCar.getJSONObject("data").getString("priceTax"));
                invoiceCarOcrDiscernVo.setSalerPhone(invoiceCar.getJSONObject("data").getString("salerPhone"));
                invoiceCarOcrDiscernVo.setSalerAddress(invoiceCar.getJSONObject("data").getString("salerAddress"));
                invoiceCarOcrDiscernVo.setEngineNum(invoiceCar.getJSONObject("data").getString("engineNum"));
                invoiceCarOcrDiscernVo.setTax(invoiceCar.getJSONObject("data").getString("tax"));
                invoiceCarOcrDiscernVo.setPurchaserCode(invoiceCar.getJSONObject("data").getString("purchaserCode"));
                invoiceCarOcrDiscernVo.setTaxAuthorCode(invoiceCar.getJSONObject("data").getString("taxAuthorCode"));
                invoiceCarOcrDiscernVo.setSalerAccountNum(invoiceCar.getJSONObject("data").getString("salerAccountNum"));
                invoiceCarOcrDiscernVo.setSalerCode(invoiceCar.getJSONObject("data").getString("salerCode"));
                return invoiceCarOcrDiscernVo;
            } else {
                log.info("OCR识别接口调用参数出错");
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * @param condition
     * @Description:获取银行卡OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: BankCardOcrDiscernVo
     * 文件类型FileCode决定返回值和调用方法
     */
    @Override
    public BankCardOcrDiscernVo getBankCardOcrDiscern(OcrDiscernCondition condition) {
        JSONObject bankCar = ocrDiscernService.bankCardOcrDiscern(condition);
        if(bankCar !=null) {
            log.info("*************************************调用接口记录入库开始**********************************");
            CaseInterfaceRecord caseInterfaceRecord = new CaseInterfaceRecord();
            caseInterfaceRecord.setBusinessNo(condition.getCaseCode());
            caseInterfaceRecord.setInterfaceType("OcrDiscern");
            caseInterfaceRecord.setReturnJson(bankCar.toString());
            this.save(caseInterfaceRecord);
            log.info("*************************************调用接口记录入库结束**********************************");
            BankCardOcrDiscernVo bankCardOcrDiscernVo = new BankCardOcrDiscernVo();
            if (bankCar.getString("code").equals("200")) {
                bankCardOcrDiscernVo.setId(bankCar.getJSONObject("data").getString("id"));
                bankCardOcrDiscernVo.setBankCardNumber(bankCar.getJSONObject("data").getString("bankCardNumber"));
                bankCardOcrDiscernVo.setValidDate(bankCar.getJSONObject("data").getDate("vaildDate"));
                bankCardOcrDiscernVo.setBankCardType(bankCar.getJSONObject("data").getString("bankCardType"));
                bankCardOcrDiscernVo.setBankName(bankCar.getJSONObject("data").getString("bankName"));
                bankCardOcrDiscernVo.setBankCardMd5(bankCar.getJSONObject("data").getString("bankCardMd5"));
                bankCardOcrDiscernVo.setCreateTime(bankCar.getJSONObject("data").getDate("createTime"));
                return bankCardOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * @param condition
     * @Description:获取身份证正面OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: IdCardFrontOcrDiscernVo
     */
    @Override
    public IdCardFrontOcrDiscernVo getIdCardFrontOcrDiscern(OcrDiscernCondition condition) {
        JSONObject idCardFront = ocrDiscernService.idCardFrontOcrDiscern(condition);
        if(idCardFront !=null) {
            log.info("*************************************调用接口记录入库开始**********************************");
            CaseInterfaceRecord caseInterfaceRecord = new CaseInterfaceRecord();
            caseInterfaceRecord.setBusinessNo(condition.getCaseCode());
            caseInterfaceRecord.setInterfaceType("OcrDiscern");
            caseInterfaceRecord.setReturnJson(idCardFront.toString());
            this.save(caseInterfaceRecord);
            log.info("*************************************调用接口记录入库结束**********************************");
            IdCardFrontOcrDiscernVo idCardFrontOcrDiscernVo = new IdCardFrontOcrDiscernVo();
            if (idCardFront.getString("code").equals("200")) {
                idCardFrontOcrDiscernVo.setId(idCardFront.getJSONObject("data").getString("id"));
                idCardFrontOcrDiscernVo.setName(idCardFront.getJSONObject("data").getString("name"));
                idCardFrontOcrDiscernVo.setSex(idCardFront.getJSONObject("data").getString("sex"));
                idCardFrontOcrDiscernVo.setNation(idCardFront.getJSONObject("data").getString("nation"));
                idCardFrontOcrDiscernVo.setBirthday(idCardFront.getJSONObject("data").getDate("birthday"));
                idCardFrontOcrDiscernVo.setAddress(idCardFront.getJSONObject("data").getString("address"));
                idCardFrontOcrDiscernVo.setNumber(idCardFront.getJSONObject("data").getString("number"));
                idCardFrontOcrDiscernVo.setIdCardFrontMd5(idCardFront.getJSONObject("data").getString("idCardFrontMd5"));
                idCardFrontOcrDiscernVo.setCreateTime(idCardFront.getJSONObject("data").getDate("createTime"));
                return idCardFrontOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * @param condition
     * @Description:获取身份证反面OCR识别
     * @Author: rongji.zhang
     * @Date: 2020/8/12 11:41
     * @Return: IdCardBackOcrDiscernVo
     */
    @Override
    public IdCardBackOcrDiscernVo getIdCardBackOcrDiscern(OcrDiscernCondition condition) {
        JSONObject idCardBack = ocrDiscernService.idCardBackOcrDiscern(condition);
        if(idCardBack !=null) {
            log.info("*************************************调用接口记录入库开始**********************************");
            CaseInterfaceRecord caseInterfaceRecord = new CaseInterfaceRecord();
            caseInterfaceRecord.setBusinessNo(condition.getCaseCode());
            caseInterfaceRecord.setInterfaceType("OcrDiscern");
            caseInterfaceRecord.setReturnJson(idCardBack.toString());
            this.save(caseInterfaceRecord);
            log.info("*************************************调用接口记录入库结束**********************************");
            IdCardBackOcrDiscernVo idCardBackOcrDiscernVo = new IdCardBackOcrDiscernVo();
            if (idCardBack.getString("code").equals("200")) {
                idCardBackOcrDiscernVo.setId(idCardBack.getJSONObject("data").getString("id"));
                idCardBackOcrDiscernVo.setValidPeriod(idCardBack.getJSONObject("data").getString("validPeriod"));
                idCardBackOcrDiscernVo.setLssue(idCardBack.getJSONObject("data").getString("lssue"));
                idCardBackOcrDiscernVo.setValidUntil(idCardBack.getJSONObject("data").getDate("validUntil"));
                try {
                    if(idCardBack.getJSONObject("data").getString("lssueDate").equals("长期")){
                        String time = "9999-12-31";
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            Date date = sdf.parse(time);
                            idCardBackOcrDiscernVo.setLssueDate(date);
                        } catch (ParseException exception) {
                            // TODO Auto-generated catch block
                            exception.printStackTrace();
                        }
                    } else {
                        idCardBackOcrDiscernVo.setLssueDate(idCardBack.getJSONObject("data").getDate("lssueDate"));
                    }
                } catch (Exception e){
                    String time = "8888-12-31";
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        Date date = sdf.parse(time);
                        idCardBackOcrDiscernVo.setLssueDate(date);
                    } catch (ParseException exception) {
                        // TODO Auto-generated catch block
                        exception.printStackTrace();
                    }
                }
                idCardBackOcrDiscernVo.setIdCardBackMd5(idCardBack.getJSONObject("data").getString("idCardBackMd5"));
                idCardBackOcrDiscernVo.setCreateTime(idCardBack.getJSONObject("data").getDate("createTime"));
                return idCardBackOcrDiscernVo;
            } else {
                log.info("接口调用参数出错");
                return null;
            }
        }else {
            return null;
        }
    }
}
